package org.eclipse.gittab.fretboard.draw2d.figures;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.geometry.Insets;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.graphics.Color;

public class FretFigure extends RectangleFigure {

	public FretFigure(double width, int height, Color bg, Color fret) {

//		ToolbarLayout layout = new ToolbarLayout();
//		setLayoutManager(layout);
		setBorder(new LeftRightBorder(fret, 2));
		setOpaque(true);
		setBackgroundColor(bg);
		
		//setSize(width, height);
		setBounds(new Rectangle(new Point(0,0),new Point(width, height)));
	}
	
	static class LeftRightBorder extends LineBorder {
		
		public LeftRightBorder(Color color, int width) {
			super(color, width, Graphics.LINE_SOLID);
		}
		
		@Override
		public void paint(IFigure figure, Graphics graphics, Insets insets) {
			
			tempRect.setBounds(getPaintRectangle(figure, insets));
			if (getWidth() % 2 == 1) {
				tempRect.width--;
				tempRect.height--;
			}
			tempRect.shrink(getWidth() / 2, getWidth() / 2);
			graphics.setLineWidth(getWidth());
			graphics.setLineStyle(getStyle());
			if (getColor() != null)
				graphics.setForegroundColor(getColor());
			
			// left
			graphics.drawLine(tempRect.x, tempRect.y, tempRect.x, tempRect.y + tempRect.height);
			// right
			graphics.drawLine(tempRect.x + tempRect.width, tempRect.y, tempRect.x + tempRect.width, tempRect.y + tempRect.height);
		}		
	}
}
